package com.wh.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.vo.AjaxResult;
import com.wh.system.annotation.PreAuthorize;
import com.wh.system.dto.PermissionDto;
import com.wh.system.pojo.Permission;
import com.wh.system.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;

    /**
     * 接口：添加或修改
     *
     * @param permission 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PreAuthorize(sn = "permission/addOrUpd",name = "添加或修改权限")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Permission permission) {
        if (permission.getId() != null) {
            permissionService.updateById(permission);
        } else {
            permissionService.add(permission);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @PreAuthorize(sn = "permission/del",name = "根据id删除权限")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        permissionService.deleteById(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "permission/patchDel",name = "批量删除权限")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        permissionService.patchDel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */

    @PreAuthorize(sn = "permission/findOne",name = "根据id查找权限")
    @GetMapping("/{id}")
    public Permission findOne(@PathVariable("id") Long id) {
        return permissionService.findOne(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */

    //@PreAuthorize(sn = "permission/findAll",name = "添加或修改权限")
    @GetMapping("/findall")
    public List<Permission> findAll() {
        return permissionService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param permissionDto 查询对象
     * @return IPage<Permission> 分页对象
     */

    @PreAuthorize(sn = "permission/findByPage",name = "分页查找权限列表")
    @PostMapping
    public IPage<Permission> findByPage(@RequestBody PermissionDto permissionDto) {
        return permissionService.findByPage(permissionDto);
    }


}
